/* created at 2008-07-02:13:20:43 */
package pl.kwiecienm.jcomet.loc;

import pl.kwiecienm.jcomet.LineAnalyzer;
import pl.kwiecienm.jcomet.model.AnalysisResult;
import pl.kwiecienm.jcomet.model.TextFile;

/**
 * @author kwiecienm
 */
public final class LOCAnalyzer implements LineAnalyzer {

    /** */
    public static final String LINES_OF_CODE     = "loc.lines_of_code";
    /** */
    public static final String LINES_OF_COMMENTS = "loc.lines_of_comments";
    /** */
    public static final String EMPTY_LINES       = "loc.empty_lines";

    /** */
    private int                _linesOfCode;
    /** */
    private int                _linesOfComments;
    /** */
    private int                _emptyLines;
    /** */
    private boolean            _blockComment;

    /**
     */
    @Override
    public void preAnalyse() {
        this._linesOfCode = this._linesOfComments = this._emptyLines = 0;
        this._blockComment = false;
    }

    /**
     */
    @Override
    public void analyse(String inLine) {
        String line = inLine.trim();

        if ((line.startsWith("//") || line.startsWith("/*") || this._blockComment)) {
            this._linesOfComments++;
        } else if (line.length() > 2) {
            this._linesOfCode++;
        } else {
            this._emptyLines++;
        }

        if (line.contains("/*")) {
            this._blockComment = true;
        }
        if (line.contains("*/")) {
            this._blockComment = false;
        }
    }

    /**
     */
    @Override
    public void postAnalyse() {
        // nothing here
    }

    /**
     */
    @Override
    public void mergeAnalysisResult(AnalysisResult inAnalysisResult) {
        inAnalysisResult.put(LINES_OF_CODE, this._linesOfCode);
        inAnalysisResult.put(LINES_OF_COMMENTS, this._linesOfComments);
        inAnalysisResult.put(EMPTY_LINES, this._emptyLines);
    }

    /**
     */
    @Override
    public boolean accepts(TextFile inFile) {
        boolean ret = false;
        if ("java".equals(inFile.getExtension())) {
            ret = true;
        }
        return ret;
    }
}
